package com.mt.mtyx.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.common.result.ResultCodeEnum;
import com.mt.mtyx.entity.sys.RegionWare;
import com.mt.mtyx.sys.service.RegionWareService;
import com.mt.mtyx.vo.sys.RegionWareQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author WMT
 * @Date 2023/7/18 10:53
 */
@Api(tags = "开通区域")
@RequestMapping("/admin/sys/regionWare")
@RestController
public class RegionWareController {

    @Autowired
    private RegionWareService regionWareService;

    @ApiOperation("分页条件查询开通区域列表")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<RegionWare>> selectAll(
            @PathVariable Long page,
            @PathVariable Long limit,
            RegionWareQueryVo queryVo
    ) throws Exception {
        return Result.success(regionWareService.selectAll(new Page<>(page, limit), queryVo));
    }

    @ApiOperation("开通区域")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody RegionWare regionWare) throws Exception {
        if (regionWareService.countByRegionId(regionWare.getRegionId()) > 0) {
            throw new BusinessException(ResultCodeEnum.REGION_OPEN);
        }
        return Result.success(regionWareService.insert(regionWare));
    }

    @ApiOperation("删除开通区域")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> delete(@PathVariable Long id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(regionWareService.delete(id));
    }

    @ApiOperation("取消开通区域")
    @PostMapping("/updateStatus/{id}/{status}")
    public Result<Integer> updateStatus(
            @PathVariable Long id,
            @PathVariable Integer status
    ) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(regionWareService.updateStatus(id, status));
    }
}
